/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alert;

/**
 *
 * @author comiang
 */
import sound.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javazoom.jl.player.Player;


public class MP3 {
    private String filename;
    private Player player; 

    // constructor that takes the name of an MP3 file
    public MP3(String filename) {
        this.filename = filename;
    }



    public void close() { if (player != null) player.close(); clock.stop(); }

    // play the MP3 file to the sound card
    public void play() {
      
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }
        

        // run in new thread to play in background
        new Thread() {
            public void run() {
            
                try { 
                   
                    player.play();
                 
                }
                catch (Exception e) { System.out.println(e); }
            }
        }.start();



        
    }

 javax.swing.Timer clock;
   
    public  void playsound() { 
    
     clock=new javax.swing.Timer(1000, new ActionListener() {
            @Override
                        public void actionPerformed(ActionEvent e) {
                                if(filename.contains(":")){
                                      MP3       mp3 = new MP3(filename);
                                    mp3.play();
                                }else{
                                     String path=System.getProperty("user.dir");
           
                             MP3       mp3 = new MP3(path+filename);
                                    mp3.play();
                                }
                            


                            }
	           } );
	           clock.start();
        
        
     

    }
}

